﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace CustomControl.CustomDataGridView {
    [ToolboxBitmap(typeof(DataGridView))]
    public partial class CustomDataGridView1 : DataGridView {

        private int allCount = 0;   //数据表总行数
        private int pageCount = 0;  //数据表总页数
        private int showRows = 10;   //一页显示的行数
        private int page = 1;   //当前页码
        private static string strConn = null;   //数据库连接字符串
        private static string tableName = null; //数据库表名

        #region 数据库连接字符串
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        [Browsable(true)]
        [Localizable(true)]
        [Category("Appearance")]
        [DefaultValue(null)]
        [DescriptionAttribute("数据库连接字符串")]
        public string StrConn {
            set {
                strConn = value;
            }
            get {
                return strConn;
            }
        }
        #endregion

        #region 数据库表名
        /// <summary>
        /// 数据库表名
        /// </summary>
        [Browsable(true)]
        [Localizable(true)]
        [Category("Appearance")]
        [DefaultValue(null)]
        [DescriptionAttribute("数据库表名")]
        public string TableName {
            set {
                tableName = value;
            }
            get {
                return tableName;
            }
        }
        #endregion

        #region 一页显示的行数
        /// <summary>
        /// 一页显示的行数
        /// </summary>
        [Browsable(true)]
        [Localizable(true)]
        [Category("Appearance")]
        [DefaultValue(null)]
        [DescriptionAttribute("一页显示的行数")]
        public int ShowRows {
            set {
                showRows = value;
            }
            get {
                return showRows;
            }
        }
        public CustomDataGridView1() {
            InitializeComponent();
        }
        #endregion

        /// <summary>
        /// 分页显示数据表内容
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void ShowPage(int page) {
            try {
                MySqlConnection conn = new MySqlConnection(strConn);
                string sql = null;
                int firstRow = 0;
                int backRow = 0;
                if (page == 1) {
                    firstRow = page;
                    backRow = page * showRows;
                } else {
                    firstRow = (page - 1) * showRows + 1;
                    backRow = (page - 1) * showRows + showRows;
                }
                sql = string.Format("select * from {0} where id between {1} and {2};", tableName, firstRow, backRow);

                MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                ds = null;
            } catch (Exception err) {
#if DEBUG
                Console.WriteLine("查询分页错误！错误信息：{0}", err);
#endif
            }
        }

        /// <summary>
        /// 初始化(计算表总页数)
        /// </summary>
        public void InitPageCount() {
            using (MySqlConnection connection = new MySqlConnection(strConn)) {
                try {
                    MySqlDataAdapter da = new MySqlDataAdapter(string.Format("select * from {0};", tableName), connection);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    allCount = dt.Rows.Count;
                    pageCount = allCount % showRows;
                    if (pageCount == 0) {
                        pageCount = allCount / showRows;
                    } else {
                        pageCount = allCount / showRows + 1;
                    }
                    this.label1.Text = string.Format("共 {0} 页", pageCount);
                    this.label3.Text = string.Format("/{0}页", pageCount);
                    ShowPage(1);
                    UpdatePage();

                } catch (Exception err) {
#if DEBUG
                    Console.WriteLine("查询错误！错误信息：{0}", err);
#endif
                }
            }
        }

        /// <summary>
        /// 更新页码
        /// </summary>
        private void UpdatePage() {
            this.label2.Text = string.Format("当前第 {0} 页", page);
            this.textBox1.Text = page.ToString();
        }

        #region 查询页
        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_FirstPage_Click(object sender, EventArgs e) {
            page = 1;
            ShowPage(1);
            UpdatePage();
        }

        /// <summary>
        /// 尾页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BackPage_Click(object sender, EventArgs e) {
            page = pageCount;
            ShowPage(pageCount);
            UpdatePage();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_upPage_Click(object sender, EventArgs e) {
            page -= 1;
            if (page < 1) {
                page = 1;
            }
            ShowPage(page);
            UpdatePage();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_downPage_Click(object sender, EventArgs e) {
            page += 1;
            if (page > pageCount) {
                page = pageCount;
            }
            ShowPage(page);
            UpdatePage();
        }

        /// <summary>
        /// 选择页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e) {
            try {
                page = Convert.ToInt32(this.textBox1.Text);
                ShowPage(page);
                UpdatePage();
            } catch (Exception) {
            }
        }
        #endregion

    }
}















